/**
 * Created by Administrator on 2017/7/10.
 */
"use strict";
const helpConst = require('./helpConst');
const async = require("async");
const fs= require('fs');
const path=require('path');

module.exports = {
    open:true,
    handler(req, res, next) {
        req.mongo.collection("api").find().toArray( (err, r)=> {
            if (err) {
                return  res.reply({message:err});
            }

            let helpArray=[];

                r.forEach((m,j)=>{
                    let api = {_id:m._id,route:m.route,name:'',open:m.open,security:m.security};
                    let method = m._id.substring(0,m._id.indexOf('#'));
                    let apiPath =  path.join(process.cwd() , 'routers/'+m._id.replace(method+'#/','').replace(':','@')+'/#'+method)+'\\readme.md';

                    console.log(apiPath,fs.existsSync(apiPath))
                    if(fs.existsSync(apiPath)){
                        api.name=fs.readFileSync(apiPath,'utf-8').replace(/\r/ig, '').replace(/\n/ig, '');
                    }else{
                        api.name='未找到描述文件';
                    }

/*                    if (helpConst.apiName[m._id]){
                        api.name = helpConst.apiName[m._id];
                    }
                    if(helpConst.routeName[m.route]){
                        api.routeName = helpConst.routeName[m.route]
                    }*/
                    helpArray.push(api);
                });

            res.reply({code:0,message:"查询成功",data:helpArray});
            })
    }
};
